package com.lc.cehui.app;

import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hjq.http.listener.OnHttpListener;
import com.lc.cehui.R;
import com.lc.cehui.action.ToastAction;
import com.lc.cehui.base.BaseFragment;
import com.lc.cehui.http.model.HttpData;
import com.lc.cehui.http.response.BaseBean;

import okhttp3.Call;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2018/10/18
 *    desc   : 业务 Fragment 基类
 */
public abstract class AppFragment<A extends AppActivity> extends BaseFragment<A>
        implements ToastAction, OnHttpListener<Object> {

    /**
     * 当前加载对话框是否在显示中
     */
    public boolean isShowDialog() {
        A activity = getAttachActivity();
        if (activity != null) {
            return activity.isShowDialog();
        }

        return false;
    }

    /**
     * 显示加载对话框
     */
//    public void showDialog() {
//        A activity = getAttachActivity();
//        if (activity != null) {
//            activity.showDialog();
//        }
//    }

    /**
     * 隐藏加载对话框
     */
    public void hideDialog() {
        A activity = getAttachActivity();
        if (activity != null) {
            activity.hideDialog();
        }
    }

    /**
     * {@link OnHttpListener}
     */

    @Override
    public void onStart(Call call) {
//        showDialog();
    }

    @Override
    public void onSucceed(Object result) {
        if (result instanceof HttpData) {
            toast(((HttpData<?>) result).getMessage());
        }
    }

    @Override
    public void onFail(Exception e) {
        toast(e.getMessage());
    }

    @Override
    public void onEnd(Call call) {
        hideDialog();
    }


    /**
     * 显示空布局
     *
     * @param adapter recyclerView的适配器
     */
    public void showEmptyView(BaseQuickAdapter adapter) {
        setAdapterView(adapter, false);
    }

    /**
     * 显示加载中布局
     *
     * @param adapter recyclerView的适配器
     */
    public void showLoadView(BaseQuickAdapter adapter) {
        setAdapterView(adapter, true);
    }
    public View emptyView;
    /**
     * 设置适配器的空布局
     *
     * @param adapter 适配器
     * @param isLoad  是否是加载中
     */
    public void setAdapterView(BaseQuickAdapter adapter, boolean isLoad) {
        //声明全局变量View emptyView;
        if (emptyView == null) {
            emptyView = getLayoutInflater().inflate(R.layout.view_empty, null);
        }
        ViewGroup parentView = (ViewGroup) emptyView.getParent();
        if (isLoad) {
            emptyView.findViewById(R.id.imageView_img).setVisibility(View.GONE);
            emptyView.findViewById(R.id.progressBar_loading).setVisibility(View.VISIBLE);
        } else {
            emptyView.findViewById(R.id.imageView_img).setVisibility(View.VISIBLE);
            emptyView.findViewById(R.id.progressBar_loading).setVisibility(View.GONE);
        }
        //数据得清空才会显示空布局
        adapter.getData().clear();
        if (emptyView.getParent() != null) {
            parentView.removeView(emptyView);
        }
        adapter.setEmptyView(emptyView);
        adapter.notifyDataSetChanged();
    }


    public boolean callBackCode(BaseBean bean) {
        boolean is = false;
        int code = bean.getCode();
        String msg = bean.getMsg();
        if (code == 200) {
            //请求成功
            is = true;
        } else {
            is = false;
            toast(msg);
        }
        return is;
    }


}